userData = userData or {}

function userData:genUserId()
    --lkj note:使用当前本地的秒数的处理值来定义id，2个同秒数登陆的新人id重复概率为1%
    local tm = os.date("*t")
    local cursec = os.time(tm)
    local id = cursec - 1400000000
    id = id*100 + math.random(100)
    return string.format("%d",id)
end

function userData:genResetSec()
    local tm = os.date("*t")
    local resetsec = os.time{year = tm.year, month = tm.month, day = tm.day, hour = 23, min = 59, sec = 59}
    resetsec = resetsec + 1
    return resetsec
end

function userData:initByDefaultData()
    userData.id = self:genUserId()
    userData.coin = pp.startCoin
    userData.lives = pp.lives_fullcnt_low
    userData.fulllives = pp.lives_fullcnt_low
    userData.rstsec = 0
    userData.lastGotLoginAwardSec = 0
    userData.continuesLoginAwardTimes = 0

    userData.resetSec = self:genResetSec()
    userData.lottery_rstsec = 0
    userData.lottery_useTime = 1

    userData.weaponLv_fenji = 1
    userData.weaponCnt_fenji = 0
    userData.weaponLv_yuheng = 1
    userData.weaponCnt_yuheng = 0
    userData.weaponLv_huanman = 1
    userData.weaponLv_boom = 1
    userData.weaponLv_colorful = 1

    userData.passedlevelinfo = {[1] = {stars = 0, score = 0}} --lkj note: 这里最后一个元素不是passed，是当前可以打的最后关卡
    userData.lockUnlocked = false

    userData.noread_fileLastModifiedSec = 0

    userData.spe_doubleFuhuoTimes = 0
    userData.spe_bought_doubleFuhuoLibao = 0
    userData.bought_xinshoulibao = false

    userData.spe_freeFuhuoLeft = 0

    userData.musicOn = true
    userData.effectOn = true
end

function userData:synLevelScoreAndStar()

    if not userData.passedlevelinfo[gameData.level] then
        userData.passedlevelinfo[gameData.level] = {}
    end
    
    local levelinfo = userData.passedlevelinfo[gameData.level]
    if levelinfo.stars<gameData.curStars then
        levelinfo.stars = gameData.curStars
    end
    if levelinfo.score < gameData.curScore then
        levelinfo.score = gameData.curScore
    end
end


function userData:unlockNextLevel()
    if gameData.level+1 <= pp.levelMax and userData.passedlevelinfo[gameData.level+1] == nil then
        userData.passedlevelinfo[gameData.level+1] = {stars = 0, score = 0}
    end
end

function userData:lives_castOne()
    userData.lives = userData.lives - 1
    if userData.lives + 1 == userData.fulllives then
        self:lives_calcNextRestoreTime()
    end
end

------------------------------------------
function userData:lottery_resetData()
    self.lottery_rstsec = 0
    self.lottery_useTime = 1
end

function userData:lottery_isFree()
    -- body
    local tm = os.date("*t")
    local sec = os.time(tm) 
    return sec >= self.lottery_rstsec
end

function userData:lottery_restoreNeedTime()
    
    -- local _levelData = require "levelData"
    if self.lottery_useTime == 1 then
        return ""
    else
        local tm = os.date("*t")
        local sec = os.time(tm) 

        if sec < self.lottery_rstsec then
            -- _levelData.lottery_bFree = true
            return subTimeString(tm, os.date("*t", self.lottery_rstsec))
        else
            return ""
        end
    end
end

function userData:lottery_calcNextRestoreTime()
    -- body
    local tm = os.date("*t")
    local sec = os.time{year = tm.year, month = tm.month, day = tm.day, hour = tm.hour, min = tm.min, sec = tm.sec}
    local cfg = require "lotteryCfg"
    self.lottery_rstsec = sec + cfg.costInfo[self.lottery_useTime].itv
end
-------------------------------------------------

function userData:lives_calcNextRestoreTime()
    local tm = os.date("*t")
    d_print(tm.year.."."..tm.month.."."..tm.day.." "..tm.hour..":"..tm.min..":"..tm.sec)

    local sec = os.time{year = tm.year, month = tm.month, day = tm.day, hour = tm.hour, min = tm.min, sec = tm.sec}
    self.rstsec = sec + pp.lives_restoreTime
end

function userData:lives_restoreNeedTime()
    if userData.lives < userData.fulllives then
        local tm = os.date("*t")
        local sec = os.time(tm)
     
        if sec < self.rstsec then
            return subTimeString(tm, os.date("*t", self.rstsec))
        else
            
            while sec >= self.rstsec do
                if userData.lives < userData.fulllives then
                    userData.lives = userData.lives + 1
                    self.rstsec = self.rstsec + pp.lives_restoreTime   
                else
                    break
                end
            end
            
            --没有恢复满，那么
            if userData.lives < userData.fulllives then
                self:lives_calcNextRestoreTime()
                return subTimeString(tm, os.date("*t", self.rstsec))
            else
                return ""
            end
        end
    else
        return ""
    end
end

function userData:checkGettingLoginAward()
    local lasttm = os.date("*t", self.lastGotLoginAwardSec)
    local tm = os.date("*t")
    local cursec = os.time(tm)
    
    if self.lastGotLoginAwardSec > cursec then
        assert(false, "userData:checkGettingLoginAward,lasttm > tm")
        self.continuesLoginAwardTimes = 0
        return pp.loginAwardStatus.got
    else
        if lasttm.year == tm.year and lasttm.month == tm.month then

            if lasttm.day == tm.day then
                return pp.loginAwardStatus.got
            elseif tm.day - lasttm.day == 1 then
                self.continuesLoginAwardTimes = self.continuesLoginAwardTimes + 1
                if self.continuesLoginAwardTimes >= 7 then
                    self.continuesLoginAwardTimes = 0
                end
                return pp.loginAwardStatus.ungot
            else
                self.continuesLoginAwardTimes = 0
                return pp.loginAwardStatus.ungot
            end

        else
            self.continuesLoginAwardTimes = 0
            return pp.loginAwardStatus.ungot
        end

    end
end


-----------------------------------------------------------------
function userData:toString()

    local s = {}
    
    s[#s+1] = "local record = {\n"
    
    s[#s+1] = "\tid = "..self.id..",\n"

    s[#s+1] = "\tpassedlevelinfo = {\n"
    for k,v in ipairs(userData.passedlevelinfo)do
        s[#s+1] = "\t\t["..k.."] = {stars="..v.stars..", score="..v.score.."},\n"
    end
    s[#s+1] = "\t},\n"
    s[#s+1] = "\tlockUnlocked="..tostring(self.lockUnlocked)..",\n"

    s[#s+1] = "\tresetSec = "..self.resetSec..",\n"

    s[#s+1] = "\tlives = "..self.lives..",\n"
    s[#s+1] = "\tfulllives = "..self.fulllives..",\n"
    s[#s+1] = "\trstsec = "..self.rstsec..",\n"
    s[#s+1] = "\tlastGotLoginAwardSec = "..self.lastGotLoginAwardSec..",\n"
    s[#s+1] = "\tcontinuesLoginAwardTimes = "..self.continuesLoginAwardTimes..",\n"
    s[#s+1] = "\tlottery_rstsec = "..self.lottery_rstsec..",\n"
    s[#s+1] = "\tlottery_useTime = "..self.lottery_useTime..",\n"


    s[#s+1] = "\tcoin = "..self.coin..",\n"
    s[#s+1] = "\tweaponLv_fenji = "..self.weaponLv_fenji..",\n"
    s[#s+1] = "\tweaponCnt_fenji = "..self.weaponCnt_fenji..",\n"
    s[#s+1] = "\tweaponLv_yuheng = "..self.weaponLv_yuheng..",\n"
    s[#s+1] = "\tweaponCnt_yuheng = "..self.weaponCnt_yuheng..",\n"
    s[#s+1] = "\tweaponLv_huanman = "..self.weaponLv_huanman..",\n"
    s[#s+1] = "\tweaponLv_boom = "..self.weaponLv_boom..",\n"
    s[#s+1] = "\tweaponLv_colorful = "..self.weaponLv_colorful..",\n"

    s[#s+1] = "\tspe_doubleFuhuoTimes = "..self.spe_doubleFuhuoTimes..",\n"
    s[#s+1] = "\tspe_bought_doubleFuhuoLibao = "..self.spe_bought_doubleFuhuoLibao..",\n"
    s[#s+1] = "\tbought_xinshoulibao = "..tostring(self.bought_xinshoulibao)..",\n"

    s[#s+1] = "\tspe_freeFuhuoLeft = "..self.spe_freeFuhuoLeft..",\n"

    s[#s+1] = "\tmusicOn = "..tostring(self.musicOn)..",\n"
    s[#s+1] = "\teffectOn = "..tostring(self.effectOn)..",\n"

    local tm = os.date("*t")
    local sec = os.time(tm)
    s[#s+1] = "\tnoread_fileLastModifiedSec = "..string.format("%d", sec)..",\n"
    
    s[#s+1] = "}\nreturn record" 
    local string = table.concat(s)
    return string
end

function userData:loadFrom(userRecord)

    --	local pli = userRecord.passedlevelinfo
    -- for k,v in ipairs(pli)do
    --     if v then
    --         self.passedlevelinfo[k] = {}
    --         local levelk = self.passedlevelinfo[k]
    --         levelk.stars = v[1]
    --         levelk.score = v[2]  
    --     end
    -- end

    self.id = userRecord.id or self:genUserId()

    assert(userRecord.passedlevelinfo, "loadFrom--passedlevelinfo == nil")
    self.passedlevelinfo = userRecord.passedlevelinfo

    if IS_ALL_LEVEL_PASSED then
        while #self.passedlevelinfo < pp.levelMax do
            self.passedlevelinfo[#self.passedlevelinfo+1] = {stars = 0, score = 0}
        end
    end
    
    --lkj temp: fixed broken recordfile
    while #self.passedlevelinfo > pp.levelMax do
        self.passedlevelinfo[#self.passedlevelinfo] = nil
    end

    self.lockUnlocked = userRecord.lockUnlocked or self.lockUnlocked

    self.resetSec = userRecord.resetSec or self:genResetSec()

    self.lives = userRecord.lives
    self.fulllives = userRecord.fulllives or pp.lives_fullcnt_low
    self.rstsec = userRecord.rstsec
    self:lives_restoreNeedTime()

    self.lastGotLoginAwardSec = userRecord.lastGotLoginAwardSec

    self.continuesLoginAwardTimes = userRecord.continuesLoginAwardTimes
    self.lottery_rstsec = userRecord.lottery_rstsec or 0
    self.lottery_useTime = userRecord.lottery_useTime or 1
    
    self.coin = userRecord.coin
    self.weaponLv_fenji = userRecord.weaponLv_fenji
    self.weaponCnt_fenji = userRecord.weaponCnt_fenji
    self.weaponLv_yuheng = userRecord.weaponLv_yuheng
    self.weaponCnt_yuheng = userRecord.weaponCnt_yuheng
    self.weaponLv_huanman = userRecord.weaponLv_huanman or self.weaponLv_huanman
    self.weaponLv_boom = userRecord.weaponLv_boom or self.weaponLv_boom
    self.weaponLv_colorful = userRecord.weaponLv_colorful or self.weaponLv_colorful

    self.spe_doubleFuhuoTimes = userRecord.spe_doubleFuhuoTimes or 0
    self.spe_bought_doubleFuhuoLibao = userRecord.spe_bought_doubleFuhuoLibao or 0

    -- if userRecord.bought_xinshoulibao == nil then
    --     self.bought_xinshoulibao = false
    -- else
    --     self.bought_xinshoulibao = userRecord.bought_xinshoulibao
    -- end
    self.bought_xinshoulibao = userRecord.bought_xinshoulibao or false

    self.spe_freeFuhuoLeft = userRecord.spe_freeFuhuoLeft or 0

    self.musicOn = (function() 
        if userRecord.musicOn == nil or userRecord.musicOn == true then 
            return true 
        else 
            return false
        end
    end)()
    self.effectOn = (function() 
        if userRecord.effectOn == nil or userRecord.effectOn == true then 
            return true 
        else 
            return false
        end
    end)()
end